﻿Public Class OCCUPATION

    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Dim PROVIDER_MASOCCUPATION As New PROVIDERSCLASS

    Private Sub OCCUPATION_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridoccupation()

        DesignForm(Me)
        DesignGrid(MASOCCUPATIONGrid)

        PROVIDER_MASOCCUPATION.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridoccupation()
        Dim objSelectDatamasoccupation As New masoccupation
        Dim MASOCCUPATION As DataTable
        MASOCCUPATION = connect.GetTable(objSelectDatamasoccupation.selectMasoccupation_)
        MASOCCUPATIONGrid.PrimaryGrid.DataSource = MASOCCUPATION
    End Sub

    Private Sub MASOCCUPATIONGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASOCCUPATIONGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสอาชีพ").Value() IsNot DBNull.Value Then
            OCCUPATIONIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสอาชีพ").Value()
        Else
            OCCUPATIONIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อาชีพ").Value() IsNot DBNull.Value Then
            OCCUPATIONTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อาชีพ").Value()
        Else
            OCCUPATIONTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            occupationStatusCheckbox.Checked = True
        Else
            occupationStatusCheckbox.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masoccupation SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `occid` = '" & OCCUPATIONIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        'connectlocal = conlocal.NewConnection()
        'If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
        '    Try
        '        Dim Sql As String
        '        connectlocal.BeginTrans()
        '        Sql = "UPDATE MASOCCUPATION SET "
        '        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
        '            Sql += "[STATUS] = 1 "
        '        Else
        '            Sql += "[STATUS] = 0 "
        '        End If
        '        Sql += "WHERE [OCCID] = '" & OCCUPATIONIDTxt.Text & "' ;"

        '        connectlocal.ExecuteNonQuery(Sql)
        '        connectlocal.CommitTrans()
        '    Catch ex As Exception
        '        connectlocal.RollbackTrans()
        '        MsgBox(ex.ToString)
        '    End Try
        'End If
        'connectlocal.Dispose()
        addOccupation.Enabled = False
    End Sub

    Private Sub addOccupation_Click_1(sender As Object, e As EventArgs) Handles addOccupation.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASOCCUPATION.PADD_ = True Then
                Dim objInsertData As New masoccupation
                objInsertData.occupationID_ = CInt(OCCUPATIONIDTxt.Text)
                objInsertData.occupationDescription_ = OCCUPATIONTxt.Text
                objInsertData.occupationStatus_ = occupationStatusCheckbox.Checked
                objInsertData.insertMasoccupationData()

                SelectDatagridoccupation()

                OCCUPATIONIDTxt.Text = ""
                OCCUPATIONTxt.Text = ""
                occupationStatusCheckbox.Checked = False
            Else
                MsgBox(PROVIDER_MASOCCUPATION.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editOccupation_Click_1(sender As Object, e As EventArgs) Handles editOccupation.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASOCCUPATION.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลอาชีพดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    Dim objEditData As New masoccupation
                    objEditData.occupationID_ = CInt(OCCUPATIONIDTxt.Text)
                    objEditData.occupationDescription_ = OCCUPATIONTxt.Text
                    objEditData.occupationStatus_ = occupationStatusCheckbox.Checked
                    objEditData.editMasoccupationData()

                    SelectDatagridoccupation()

                    OCCUPATIONIDTxt.Text = ""
                    OCCUPATIONTxt.Text = ""
                    occupationStatusCheckbox.Checked = False
                End If
            Else
                MsgBox(PROVIDER_MASOCCUPATION.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(masoccupation.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(masoccupation.Text)
        nextform.ShowDialog()
    End Sub
End Class